package com.example.backend.controller;

import com.example.backend.dto.AnnouncementDto;
import com.example.backend.entity.Announcement;
import com.example.backend.service.AnnouncementService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/announcements")
@RequiredArgsConstructor
public class AnnouncementController {

    private final AnnouncementService announcementService;

    @PostMapping
    public ResponseEntity<AnnouncementDto.Response> createAnnouncement(
            @Valid @RequestBody AnnouncementDto.CreateRequest request
    ) {
        AnnouncementDto.Response response = announcementService.createAnnouncement(request);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    @GetMapping("/{id}")
    public ResponseEntity<AnnouncementDto.Response> getAnnouncementById(@PathVariable Long id) {
        AnnouncementDto.Response response = announcementService.getAnnouncementById(id);
        return ResponseEntity.ok(response);
    }

    @GetMapping
    public ResponseEntity<Page<AnnouncementDto.Response>> getAllAnnouncements(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "createdAt") String sortBy,
            @RequestParam(defaultValue = "DESC") Sort.Direction direction
    ) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sortBy));
        Page<AnnouncementDto.Response> announcements = announcementService.getAllAnnouncements(pageable);
        return ResponseEntity.ok(announcements);
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<Page<AnnouncementDto.Response>> getAnnouncementsByStatus(
            @PathVariable Announcement.AnnouncementStatus status,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "createdAt") String sortBy,
            @RequestParam(defaultValue = "DESC") Sort.Direction direction
    ) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sortBy));
        Page<AnnouncementDto.Response> announcements = announcementService.getAnnouncementsByStatus(status, pageable);
        return ResponseEntity.ok(announcements);
    }

    @GetMapping("/published")
    public ResponseEntity<List<AnnouncementDto.Response>> getPublishedAnnouncements() {
        List<AnnouncementDto.Response> announcements = announcementService.getPublishedAnnouncements();
        return ResponseEntity.ok(announcements);
    }

    @GetMapping("/author/{authorId}")
    public ResponseEntity<Page<AnnouncementDto.Response>> getAnnouncementsByAuthor(
            @PathVariable Long authorId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "createdAt") String sortBy,
            @RequestParam(defaultValue = "DESC") Sort.Direction direction
    ) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sortBy));
        Page<AnnouncementDto.Response> announcements = announcementService.getAnnouncementsByAuthor(authorId, pageable);
        return ResponseEntity.ok(announcements);
    }

    @PutMapping("/{id}")
    public ResponseEntity<AnnouncementDto.Response> updateAnnouncement(
            @PathVariable Long id,
            @Valid @RequestBody AnnouncementDto.UpdateRequest request
    ) {
        AnnouncementDto.Response response = announcementService.updateAnnouncement(id, request);
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAnnouncement(@PathVariable Long id) {
        announcementService.deleteAnnouncement(id);
        return ResponseEntity.noContent().build();
    }
}
